package blog.hibernate;

import java.util.Collection;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import blog.model.Blog;

/**
 * @author Hakdo
 */
@Transactional
@Repository
public class BlogHibernate {
	
	@Autowired private transient SessionFactory sessionFactory;

	public void createBlog(final Blog blog) {
		sessionFactory.getCurrentSession().save(blog);
	}
	
	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true)
	public Collection<Blog> findBlogs() {
		return sessionFactory.getCurrentSession().createCriteria(Blog.class).list();
	}
	
	@Transactional(readOnly = true)
	public Blog findBlog(final int blogId) {
		return (Blog) sessionFactory.getCurrentSession().get(Blog.class, blogId);
	}
	
	public void updateBlog(final Blog blog) {
		sessionFactory.getCurrentSession().update(blog);
	}
	
	public void deleteBlog(final int blogId) {
		sessionFactory.getCurrentSession().delete(findBlog(blogId));
	}
}
